package divestoclimb.checklist.data;

import divestoclimb.checklist.storage.DbAdapter;

public class Category {

	protected long mID;		// The ID of this category in the database
	protected String mName;	// The name of this category in the database
	protected boolean mDirty;
	
	// Getters and setters
	public long getID() { return mID; }
	protected void setID(long id) { mID=id; }
	public String getName() { return mName; }
	public void setName(String name) { mName=name; mDirty=true; }
	
	// Constructors
	public Category(String name) {
		mID=-1;
		mName=name;
		mDirty=true;
	}
	
	public Category(long id, String name) {
		mID=id;
		mName=name;
		mDirty=false;
	}
	
	public boolean save(DbAdapter d) {
		if(! mDirty) { return false; }
		boolean success;
		if(mID == -1) {
			mID = d.createCategory(this);
			success = mID != -1;
		} else {
			success = d.updateCategory(this);
		}
		mDirty = ! success;
		return success;
	}
}
